/*
 * StringTemplateViewServlet.java
 *
 * Copyright 2010 Jack Leow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package my.edu.clhs.stringtemplate.servlet.view;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import my.edu.clhs.stringtemplate.servlet.ServletContextStringTemplateGroup;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet for presenting content using StringTemplate as the templating engine.
 *
 * <p>With this implementation, each page has its own StringTemplate (.st) file
 * in the <tt>templateRootPath</tt> (relative to web app root). The template for
 * a request is resolved using its servletPath and pathInfo, with its extension
 * stripped and replaced with <tt>.st</tt>.
 *
 * <p><b><a name="init-params">Configuration <tt>init-param</tt>s</a>
 * (<a href="AbstractStringTemplateServlet.html#init-param">
 * In addition to the superclass</a>)</b>:
 * <table border="1">
 *  <tr>
 *      <th>param-name</th>
 *      <th>Description</th>
 *  </tr>
 *  <tr>
 *      <td>templateRootPath</td>
 *      <td>
 *          The path, relative to the web application context, to the directory
 *          containing the template files.
 *          Defaults to <tt>/WEB-INF/stringtemplate</tt>.
 *      </td>
 *  </tr>
 *  <!--
 *  <tr>
 *      <td></td>
 *      <td>
 *      </td>
 *  </tr>
 *  -->
 * </table>
 *
 * @author Jack Leow
 * @version 1.0
 * @since 1.0 (Feb 20, 2010, 9:44:48 AM)
 */
public class StringTemplateViewServlet extends AbstractStringTemplateServlet {
    public static final String DEFAULT_TEMPLATE_ROOT_PATH =
        "/WEB-INF/stringtemplate";
    
    private static final String TEMPLATE_ROOT_PATH_PARAM =
        "templateRootPath";
    
    private static final Logger log =
        LoggerFactory.getLogger(StringTemplateViewServlet.class);
    private static final long serialVersionUID = 1L;
    
    private String templateRootPath = DEFAULT_TEMPLATE_ROOT_PATH;
    
    protected String getTemplateRootPath() {
        return templateRootPath;
    }
    
    // @Override
    protected StringTemplateGroup createTemplateLoader() {
        StringTemplateGroup templateLoader =
            new ServletContextStringTemplateGroup(
            templateRootPath, getServletContext());
        
        getStringTemplateSupport().
            appendServletUtilTemplateGroup(templateLoader);
        
        String templateFileEncoding = getTemplateFileEncoding();
        if (templateFileEncoding != null) {
            templateLoader.setFileCharEncoding(templateFileEncoding);
        }
        
        return templateLoader;
    }
    
    // @Override
    public void init() throws ServletException {
        log.debug("entering init()");
        super.init();
        ServletConfig config = getServletConfig();
        
        String templateRootPath =
            config.getInitParameter(TEMPLATE_ROOT_PATH_PARAM);
        if (templateRootPath != null) {
            this.templateRootPath = templateRootPath;
        }
        log.debug("exiting init()");
    }
    
    /**
     * Loads the template specified by the templateName
     */
    // @Override
    protected StringTemplate loadTemplate(String templateName) {
        log.debug("entering loadTemplate(String)");
        log.debug("exiting loadTemplate(String)");
        return getStringTemplateSupport().loadViewTemplate(
            templateName, getTemplateLoader());
    }
}